#include "stdafx.h"
#include "Progress.h"
#include <httpdownload.h>
#include <string>
#include <CommonHelp.h>
#include <CommonHelp.cpp>
using namespace std;
Progress::Progress(QWidget* parent)
	: QWidget(parent)
    , embeddedProgress(nullptr)
    , start(nullptr)
    , reset(nullptr)
    , taskTimer(nullptr)
    , download(nullptr)
    , progressValue(0)
   // , lineEdit(nullptr)

    
{
    setWindowTitle("我的进度条");
    setFixedSize(400, 200);
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
   
    download = new HttpDownload(this);



    //if (download) {
    //    download->startDownload("https://sfdl.360safe.com/360zip_setup.exe", "D:/123pan/Blender4.5/360zip_setup.exe");
    //}

      // 连接下载进度信号到进度条更新
       // 连接下载进度信号到进度条更新
    //      发送者对象指针,                  信号                   接收者对象指针,                槽
    connect(download, &HttpDownload::downloadProgress, this, &Progress::onDownloadProgress);

    // 连接下载完成信号
    connect(download, &HttpDownload::downloadFinished, this, &Progress::onDownloadFinished);

    // 连接下载错误信号
    connect(download, &HttpDownload::downloadError, this, &Progress::onDownloadError);


    // 嵌入式进度条
    QLabel* embeddedLabel = new QLabel("总体进度:");
    embeddedProgress = new QProgressBar();
    embeddedProgress->setRange(0, 100);
    embeddedProgress->setValue(0);
    embeddedProgress->setFormat("总体进度: %p%");

    QHBoxLayout* QGBl = new QHBoxLayout();
    start = new QPushButton("开始");
    reset = new QPushButton("重置");

    QGBl->addWidget(start);
    QGBl->addWidget(reset);

    lineEdit = new QLineEdit();
    lineEdit->setPlaceholderText("请输入值");
    lineEdit->setMaxLength(500);

    lineEdit = new QLineEdit();
    lineEdit->setPlaceholderText("请输入保存地址");
    lineEdit->setMaxLength(500);

    mainLayout->addWidget(lineEdit);

    mainLayout->addLayout(QGBl);
    mainLayout->addWidget(embeddedProgress);

 

    connect(start, &QPushButton::clicked, this, &Progress::startProgress);
    connect(reset, &QPushButton::clicked, this, &Progress::resetProgress);
    connect(taskTimer, &QTimer::timeout, this, &Progress::updateProgress);

    this->show();
   // taskTimer = new QTimer(this);
	// 创建进度条
	//QProgressBar* progressBar = new QProgressBar(this);

	//// 设置范围 (默认为0到100)
	//progressBar->setMinimum(0);
	//progressBar->setMaximum(100);
	//// 或者使用 setRange(0, 100);

	//// 设置当前进度
	//progressBar->setValue(50);
}


void Progress::startProgress()
{

   QString url = lineEdit->text();
   string fileName=CommonHelp::getFilenameFromUrlComplete(url.toStdString());
  
   QString filePath = QString("D:/123pan/Blender4.5/%1").arg(fileName);
	download->startDownload(QUrl(url), filePath);
  //  download.startDownload("https://sfdl.360safe.com/360zip_setup.exe", "D:/123pan/Blender4.5/360zip_setup.exe");
    // 正确的信号槽连接方式



    //  embeddedProgress->setValue(0);


      // taskTimer->start(100);

}
void Progress::updateProgress()
{
    // QTimer* taskTimer = new QTimer(this);
    qDebug() << "按钮1被点击了!";


   
    progressValue += 2;
    embeddedProgress->setValue(progressValue);
    if (progressValue >= 100) {
        taskTimer->stop();
        //taskTimer->deleteLater();
       // taskTimer = nullptr;
    }

}

void Progress::resetProgress()
{
    // progressValue = 0;
    //taskTimer->stop();
    // QTimer* taskTimer = new QTimer(this);
    embeddedProgress->setValue(0);

}
Progress::~Progress()
{
}

// 在cpp文件中实现
void Progress::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
 
    // 计算下载百分比
    if (bytesTotal > 0) {
        int progress = static_cast<int>((bytesReceived * 100) / bytesTotal);

        // 更新进度条值
        embeddedProgress->setValue(progress);

        // 更新进度条文本显示更详细的信息
        QString text = QString("%1% (%2/%3 KB)")
            .arg(progress)
            .arg(bytesReceived / 1024)
            .arg(bytesTotal / 1024);
        embeddedProgress->setFormat(text);

        qDebug() << "下载进度:" << progress << "%";
    }
    else {
        // 如果不知道总大小，显示忙碌状态
        embeddedProgress->setRange(0, 0); // 设置为忙碌状态
        qDebug() << "下载中，大小未知，已下载:" << bytesReceived << "bytes";
    }
}

void Progress::onDownloadFinished(const QString& filePath)
{
    // 下载完成，设置进度为100%
    embeddedProgress->setValue(100);
    embeddedProgress->setFormat("下载完成 100%");

    // 恢复进度条为正常模式
    embeddedProgress->setRange(0, 100);

    // 更新按钮状态
    start->setEnabled(false);
    reset->setEnabled(true);

    qDebug() << "下载完成，文件保存至:" << filePath;

    // 可以显示完成消息
    QMessageBox::information(this, "下载完成", QString("文件已下载到:\n%1").arg(filePath));
}

void Progress::onDownloadError(const QString& errorString)
{
    // 下载出错，重置进度条
    embeddedProgress->setValue(0);
    embeddedProgress->setFormat("下载失败: 0%");

    // 恢复进度条为正常模式
    embeddedProgress->setRange(0, 100);

    // 更新按钮状态
    start->setEnabled(true);
    reset->setEnabled(true);

    qDebug() << "下载错误:" << errorString;

    // 显示错误消息
    QMessageBox::critical(this, "下载错误", errorString);
}
